﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS_Server;

namespace SMS_Server_Manager
{
    public partial class AddFunction : Form
    {
        public AddFunction()
        {
            InitializeComponent();
        }

        private Form Sender;

        public System.Windows.Forms.DialogResult ViewDialog(Form sender)
        {
            Sender = sender;
            return this.ShowDialog(sender);
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            if (validators.IsValidString(FunctionName.Text) == false || FunctionName.Text.IndexOf(' ') >= 0 || FunctionName.Text.IndexOf('-') >= 0 || FunctionName.Text.IndexOf('-') >= 0 || FunctionName.Text.IndexOf('.') >= 0)
            {
                MessageBox.Show("Invalid function Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string paramFormat = ParamFormat.Text.Replace("[", "").Replace("]", "");
            if (validators.IsValidString(paramFormat) == false || paramFormat.IndexOf('-') >= 0 || paramFormat.IndexOf('-') >= 0 || paramFormat.IndexOf('.') >= 0)
            {
                MessageBox.Show("Invalid param format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string[] strs = paramFormat.Split(' ');
            paramFormat = "";
            foreach (string str in strs)
            {
                if (paramFormat != "") paramFormat += " ";
                paramFormat += "[" + str + "]";
            }
            ParamFormat.Text = paramFormat;

            bool FunctionFound = false;
            foreach (Function f in Global.Functions)
            {
                if (f.FunctionName.ToUpper() == FunctionName.Text.ToUpper())
                {
                    FunctionFound = true;
                    break;
                }
            }
            if (!FunctionFound)
            {
                Function f = new Function();
                f.FunctionName = FunctionName.Text.ToUpper();
                f.ParamFormat=paramFormat;
                f.FunctionID = 0;
                Global.Functions.Add(f);
                DataAccess DA = new DataAccess(Global.GetConfigurationDatabaseName(), Global.DatabasePassword);
                DA.CommandString = "INSERT INTO Functions (FunctionName, ParamFormat) VALUES (@FunctionName, @ParamFormat);";
                DA.AddParameter("@FunctionName", f.FunctionName);
                DA.AddParameter("@ParamFormat", f.ParamFormat);
                DA.ExecuteNonQuery();
                DA.CommandString = "SELECT FunctionID FROM Functions WHERE FunctionName=@FunctionName;";
                DA.ExecuteReader();
                if (DA.Reader.Read())
                {
                    f.FunctionID = (int)DA.Reader.GetInt64(0);
                }
                DA.Reader.Close();
                DA.Close();
                Mainform m = (Mainform)Sender;
                m.FunctionList.Items.Add(f);
                Global.unsaved_changes = true;
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Function already exist in database", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

    }
}
